#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;

int t, n;
int a[MAXN], tr[MAXN];

inline int lowbit(int x) {
    return x & (-x);
}

void add(int i, int d) {
    while (i <= n) {
        tr[i] += d;
        i += lowbit(i);
    }
}

ll sum(int i) {
    ll res = 0;
    while (i > 0) {
        res += tr[i];
        i -= lowbit(i);
    }
    return res;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while (t--) {
        cin >> n;
        ll ans = 0;
        for (int i = 1; i <= n; i++) tr[i] = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            add(a[i], 1);
            ans += i - 1 - sum(a[i] -1);
        }

        cout << ans << endl;
    }
    return 0;
}